package com.company;


import lombok.extern.slf4j.Slf4j;

/**
 * @date : 2022/11/26 00:08
 */

@Slf4j
public class P033_ReflectSelect {

    public static void main(String[] args) {
        Class<Object> select1 = select(1);
        Class<Object> select2 = select(2);
        try {
            Apple a = (Apple) select1.newInstance();
            a.eat();
            Banana b = (Banana) select2.newInstance();
            b.eat();
        } catch (Exception e) {
            log.error("异常", e);
        }
    }

    public static <T> Class<T> select(int code) {
        Class clazz;
        T t;
        switch (code) {
            case 1:
                clazz = Apple.class;
                break;
            case 2:
            default:
                clazz = Banana.class;
        }
        return clazz;
    }


    public static class Apple {
        public void eat() {
            System.out.println("eat apple");
        }
    }

    public static class Banana {
        public void eat() {
            System.out.println("eat banana");
        }
    }
}
